package interview;

public class interview_01_05 {
    public boolean oneEditAway(String first, String second) {
        int len1=first.length();
        int len2=second.length();
        if(len1<=1&&len2<=1) return true;
        if(Math.abs(len1-len2)>=2) return false;
        char[] a=first.toCharArray();
        char[] b=second.toCharArray();
        return search(a,b,0,0,false);
    }
    public boolean search(char[] a,char[] b,int index1,int index2,boolean flag){
        if(index1==a.length&&index2== b.length) return true;
        if(!flag&&a.length+b.length-index1-index2==1) return true;
        if(index1==a.length||index2== b.length) return false;
        if(flag){
            if(a[index1]!=b[index2]) return false;
            return search(a, b, index1+1, index2+1, flag);
        }
        else{
            if(a[index1]!=b[index2]) return search(a, b, index1+1, index2+1, true)||search(a, b, index1+1, index2, true)||
                    search(a, b, index1, index2+1, true);
            else return search(a, b, index1+1, index2+1, false)||search(a, b, index1+1, index2, true)||
                    search(a, b, index1, index2+1, true);
        }
    }

}
